'use strict';

let fs = require('fs');

function findRouter(path) {
    path = path || './controllers';
    let files = fs.readdirSync(path);
    return files.filter(fileName => {
        return fileName.endsWith('.js') && fileName !== 'index.js';
    })
}

function registryRouter(files, router) {
    files.forEach(item => {
        let obj = require('../controllers/' + item);
        for (let key in obj) {
            let tmpArr = key.split(' ');

            let method = tmpArr[0];
            let url = tmpArr[1];
            let fn = obj[key];

            if (method === 'get') {
                router.get(url, fn);
            } else if (method === 'post') {
                router.post(url, fn);
            } else if (method === 'put') {
                router.put(url, fn);
            } else if (method === 'del') {
                router.delete(url, fn);
            }
        }
    })
}


module.exports = {
    findRouter,
    registryRouter
}